<template>
    <page-wrapper>
        <table-search @search="search(1)" @reset="reset()">
            <template slot="form">
                <form-item label="">
                    <a-input v-model.trim="form.attrName" placeholder="名称" />
                </form-item>
            </template>
            <template slot="extra">
                <a-button class="filter-item" type="primary" icon="plus" @click="add()" :disabled="hasPerm(['product:dict:add'])">新增字典</a-button>
                <a-button
                    class="filter-item btn_space"
                    type="danger"
                    @click="remove()"
                    :disabled="!selectionBox.length || hasPerm(['product:dict:remove'])"
                    icon="delete"
                    >删除</a-button
                >
            </template>
        </table-search>
        <table-list :loading="loading" :columns="columns" rowKey="id" :pagination="pagination" :data="list" :selection="selection">
            <template slot="dataType" slot-scope="{ text }">
                <field-tag
                    :value="text"
                    type="value"
                    showType="name"
                    :data="dataTypes"
                ></field-tag>
            </template>

            <template slot="attType" slot-scope="{ text }">
                <field-tag
                    :value="text"
                    type="value"
                    showType="name"
                    :data="paramTypes"
                ></field-tag>
            </template>

            <template slot="action" slot-scope="{ text }">
                <a-button size="small" type="primary" @click="update(text)" :disabled="hasPerm(['product:dict:update'])">编辑</a-button>
                <a-button size="small" type="danger" class="btn_space" @click="remove(text)" :disabled="hasPerm(['product:dict:remove'])">删除</a-button>
            </template>
        </table-list>
        <template v-if="isShow">
            <show-info :info="info" @cancel="isShow = false" @submit="submitTypes" />
        </template>
    </page-wrapper>
</template>
<script>
import table from '@/mixins/table';
import { columns } from './columns';
import showInfo from './components/info';
import { paramTypes, dataTypes } from '@/views/product/common/types';
import { remoteList, deleteInfo } from '@/api/product/dictionary';

export default {
    name: 'UserList',
    mixins: [table],
    components: {
        showInfo
    },
    data() {
        let originForm = {
            attrName: ''
        };
        return {
            paramTypes,
            dataTypes,
            originForm,
            form: {
                ...originForm
            },
            columns,
            isShow: false,
            info: {},
            parentId: ''
        };
    },
    created(){
        this.search(1)
    },
    methods: {
        async search(page) {
            try {
                this.loading = true;
                if (page) {
                    this.form.page = page;
                }
                this.list = [];
                let res = await remoteList(this.form);
                let { list, total } = res.data;
                this.list = list;
                this.total = total;
            } finally {
                this.loading = false;
            }
        },
        //新增
        add() {
            this.isShow = true;
            this.info = {};
        },
        //修改
        update(item) {
            this.isShow = true;
            this.info = item;
        },
        //删除
        remove(item='') {
            this.$confirm({
                title: '提示',
                content: '确定要删除字典属性？',
                okText: '确定',
                cancelText: '取消',
                onOk: async () => {
                    try {
                        let ids = [];
                        if (item) {
                            ids = [item.id];
                        } else {
                            let selectionBox = this.selectionBox;
                            console.log(selectionBox);
                            selectionBox.map(async item => {
                                ids.push(item.id);
                            });
                        }
                        await deleteInfo({ ids });
                        this.$message.success('删除成功!');
                        this.search(1);
                    } catch (err) {}
                },
                onCancel() {}
            });
        },
        submitTypes() {
            this.isShow = false;
            this.info = {};
            this.search(1);
        }
    }
};
</script>
